package com.core.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.Enums.ResponseEnum;
import com.common.exception.CourseException;
import com.common.util.CopyUtil;
import com.core.flowable.domain.SysListener;
import com.core.flowable.dto.SysListenerDto;
import com.core.flowable.mapper.SysListenerMapper;
import com.core.flowable.service.SysListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SysListenerServiceImp extends ServiceImpl<SysListenerMapper, SysListener> implements SysListenerService{

   @Autowired
   private SysListenerMapper  sysListenerMapper;
  
   @Override
    public ResponseEntity list(Page pageDto,  SysListener sysListener) {
        IPage<SysListener> page = sysListenerMapper.selectPage(new Page<>(pageDto.getCurrent(), pageDto.getSize()),
                new LambdaQueryWrapper<SysListener>(sysListener));
        pageDto.setTotal(page.getTotal());
        List<SysListenerDto> sysListenerDtoList = CopyUtil.copyList(page.getRecords(), SysListenerDto.class);
        pageDto.setRecords(sysListenerDtoList);
        return ResponseEntity.ok(pageDto);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @Override
    public ResponseEntity save( SysListenerDto sysListenerDto) {
        SysListener sysListener = CopyUtil.copy(sysListenerDto, SysListener.class);
        if (StringUtils.isEmpty(sysListenerDto.getId())) {
            return this.insert(sysListener);
        } else {
            return this.update(sysListener);
        }

    }

    /**
     * 更新
     */
    private ResponseEntity update(SysListener sysListener) {
        int i = sysListenerMapper.updateById(sysListener);
        if (i <= 0) {
            throw new CourseException(ResponseEnum.UPDATE_ERROR);
        }
        return ResponseEntity.ok(ResponseEnum.UPD_SUCCESS.getDesc());
    }

    /**
     * 新增
     */
    private ResponseEntity insert(SysListener sysListener) {
        int i = sysListenerMapper.insert(sysListener);
        if (i <= 0) {
            throw new CourseException(ResponseEnum.ADD_ERROR);
        }
        return ResponseEntity.ok(ResponseEnum.ADD_SUCCESS.getDesc());
    }


    /**
     * 删除
     */
    @Override
    public ResponseEntity delAll(List<Long> ids) {
        int i = sysListenerMapper.deleteBatchIds(ids);
        if (i <= 0) {
            throw new CourseException(ResponseEnum.DELETE_ERROR);
        }
        return ResponseEntity.ok(ResponseEnum.DEL_SUCCESS.getDesc());
    }


}